﻿using Admin.NET.Core.Service;
using Admin.NET.Application.Const;
using Admin.NET.Application.Entity;
using Microsoft.AspNetCore.Http;
namespace Admin.NET.Application;
/// <summary>
/// 学年学期服务
/// </summary>
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
public class XuenianXueqiService : IDynamicApiController, ITransient
{
    private readonly SqlSugarRepository<XuenianXueqi> _rep;
    public XuenianXueqiService(SqlSugarRepository<XuenianXueqi> rep)
    {
        _rep = rep;
    }

    /// <summary>
    /// 分页查询学年学期
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Page")]
    public async Task<SqlSugarPagedList<XuenianXueqiOutput>> Page(XuenianXueqiInput input)
    {
        var query = _rep.AsQueryable()
            .WhereIF(!string.IsNullOrWhiteSpace(input.SearchKey), u =>
                u.Code.Contains(input.SearchKey.Trim())
                || u.Fullname.Contains(input.SearchKey.Trim())
            )
            .WhereIF(input.Xuenian>0, u => u.Xuenian == input.Xuenian)
            .WhereIF(input.Xueqi>0, u => u.Xueqi == input.Xueqi)
            .WhereIF(!string.IsNullOrWhiteSpace(input.Code), u => u.Code.Contains(input.Code.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.Fullname), u => u.Fullname.Contains(input.Fullname.Trim()))
            .Select<XuenianXueqiOutput>();
        if(input.StartDateRange != null && input.StartDateRange.Count >0)
        {
            DateTime? start= input.StartDateRange[0]; 
            query = query.WhereIF(start.HasValue, u => u.StartDate > start);
            if (input.StartDateRange.Count >1 && input.StartDateRange[1].HasValue)
            {
                var end = input.StartDateRange[1].Value.AddDays(1);
                query = query.Where(u => u.StartDate < end);
            }
        } 
        return await query.OrderBuilder(input).ToPagedListAsync(input.Page, input.PageSize);
    }

    /// <summary>
    /// 增加学年学期
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Add")]
    public async Task<long> Add(AddXuenianXueqiInput input)
    {
        var entity = input.Adapt<XuenianXueqi>();
        await _rep.InsertAsync(entity);
        return entity.Id;
    }

    /// <summary>
    /// 删除学年学期
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Delete")]
    public async Task Delete(DeleteXuenianXueqiInput input)
    {
        var entity = await _rep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
        //await _rep.FakeDeleteAsync(entity);   //假删除
        await _rep.DeleteAsync(entity);   //真删除
    }

    /// <summary>
    /// 更新学年学期
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Update")]
    public async Task Update(UpdateXuenianXueqiInput input)
    {
        var entity = input.Adapt<XuenianXueqi>();
        await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
    }

    /// <summary>
    /// 获取学年学期
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "Detail")]
    public async Task<XuenianXueqi> Detail([FromQuery] QueryByIdXuenianXueqiInput input)
    {
        return await _rep.GetFirstAsync(u => u.Id == input.Id);
    }

    /// <summary>
    /// 获取学年学期列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "List")]
    public async Task<List<XuenianXueqiOutput>> List([FromQuery] XuenianXueqiInput input)
    {
        return await _rep.AsQueryable().Select<XuenianXueqiOutput>().ToListAsync();
    }

    [HttpGet]
    [ApiDescriptionSettings(Name = "GetCurXueqi")]
    public async Task<XuenianXueqiOutput> GetCurXueqi()
    {
        return await _rep.AsQueryable().Where(u => u.IsCurrent == true).Select<XuenianXueqiOutput>().FirstAsync();
    }


}

